import 'package:flutter/material.dart';

import '../resources.dart';

/// created by hcl
/// on 2024/4/5
/// page LightText
/// desc 搜索词的高亮
class LightText extends StatelessWidget {
  final String content;
  final String lightText;
  final int maxLines;
  final FontWeight weight;

  const LightText(this. content, this.lightText,
      {super.key, this. maxLines = 1, this. weight = FontWeight.bold});

  @override
  Widget build(BuildContext context) {
    List<InlineSpan> spans = [];
    if (lightText.isNotEmpty) {
      var indexOf = content.indexOf(lightText);
      if (indexOf > -1) {
        if (indexOf > 0) {
          spans.add(TextSpan(
              style: TextStyle(
                  color: ColorUtils.color_333333,
                  fontSize: 14,
                  fontWeight: weight),
              text: content.substring(0, indexOf)));
        }
        spans.add(TextSpan(
            style: TextStyle(
                color: ColorUtils.color_3C6EF0, fontSize: 14, fontWeight: weight),
            text: lightText));
        spans.add(TextSpan(
            style: TextStyle(
                color: ColorUtils.color_333333, fontSize: 14, fontWeight: weight),
            text: content.substring(indexOf + lightText.length)));
      } else {
        spans.add(TextSpan(
            style: TextStyle(
                color: ColorUtils.color_333333, fontSize: 14, fontWeight: weight),
            text: content));
      }
    } else {
      spans.add(TextSpan(
          style: TextStyle(
              color: ColorUtils.color_333333, fontSize: 14, fontWeight: weight),
          text: content));
    }
//    return spans;
    return Text.rich(
      TextSpan(children: spans),
      maxLines: maxLines,
      overflow: TextOverflow.ellipsis,
    );
  }
}
